<template>
	<view class="container">
		<!-- 页面标题 -->
		<view class="page-title">选择报价 - {{ productModel }}</view>

		<!-- 报价列表 -->
		<view class="quote-section">
			<view class="section-title">报价列表</view>

			<view class="quote-list">
				<view class="quote-item" v-for="quote in quotes" :key="quote.id"
					:class="{ selected: selectedQuoteId === quote.id }" @click="selectQuote(quote.id)">
					<view class="quote-header">
						<text class="quote-user">报价用户ID：{{ quote.user_id }}</text>
					</view>

					<view class="quote-details">
						<view class="detail-item">
							<text class="detail-label">单价:</text>
							<text class="detail-value">{{ quote.ratio_quote_price }}元</text>
						</view>
						<view class="detail-item">
							<text class="detail-label">数量:</text>
							<text class="detail-value">{{ quote.quote_quantity }}个</text>
						</view>
					</view>

					<view class="quote-remark">
						<text class="remark-label">备注:</text>
						<text class="remark-content">{{ quote.note }}</text>
					</view>

					<view class="quote-image" v-if="quote.image_url">
						<text class="image-label">图片:</text>
						<text class="image-link" @click.stop="viewImage(quote.image_url)">[查看]</text>
					</view>

					<view class="quote-selector">
						<view class="radio" :class="{ checked: selectedQuoteId === quote.id }"></view>
					</view>
				</view>
			</view>

			<view class="no-quotes" v-if="quotes.length === 0">
				暂无报价
			</view>
		</view>

		<!-- 底部操作按钮 -->
		<view class="action-buttons">
			<view class="action-btn cancel" @click="cancelSelect">取消选择</view>
			<view class="action-btn confirm" @click="confirmSelect" :class="{ disabled: !selectedQuoteId }">
				确认选择
			</view>
		</view>

		<!-- 图片预览弹窗 -->
		<uv-popup :show="showImagePreview" @close="closeImagePreview" mode="center" round="10">
			<view class="image-preview-popup">
				<view class="popup-header">
					<text class="popup-title">图片预览</text>
					<text class="close-btn" @click="closeImagePreview">×</text>
				</view>
				<view class="popup-content">
					<image :src="currentImage" class="preview-image" mode="widthFix"></image>
				</view>
			</view>
		</uv-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				productId: '',
				productModel: '',
				selectedQuoteId: 0,
				showImagePreview: false,
				currentImage: '',

				loading: false,
				quotes: [],
				reload: true,
				currentPage: 1, //当前页码
				lastPage: 1, //总页码
				status: 'loading',
				contentText: {
					contentdown: ' ',
					contentrefresh: '正在加载...',
					contentnomore: '没有更多数据了'
				},
			};
		},
		onLoad(options) {
			console.log(1111, options)
			this.productId = options.productId;
			this.productModel = options.productModel;
			if (uni.getStorageSync('wanlshop:selectedQuote:productId:' + this.productId)) {
				this.selectedQuoteId = uni.getStorageSync('wanlshop:selectedQuote:productId:' + this.productId).quote.id ??
					0;
			}
			this.resetPage();
		},
		onPullDownRefresh() {
			this.resetPage();
		},
		onReachBottom() {
			if (this.currentPage >= this.lastPage) {
				this.status = 'noMore';
			} else {
				this.reload = false;
				this.currentPage = this.currentPage + 1; //页码+1
				this.status = 'loading';
				this.loadData();
			}
		},
		methods: {
			loadData() {
				uni.$uv.http.get('/inquiry/quotesList', {
					params: {
						inquiry_goods_id: this.productId
					}
				}).then(res => {
					const data = res.res.data;
					if (res.res.code === 1) {
						this.quotes = data
					} else {
						this.$wanlshop.msg(res.res.msg, 2000, true, 'none');
					}
					this.loading = false;
				}).catch(error => {
					console.log('error', error)
					this.$wanlshop.msg('获取失败，请稍后重试', 2000, true)
					this.loading = false;
				});
			},

			resetPage() {
				this.reload = true;
				this.status = 'loading';
				this.currentPage = 1;
				this.loadData();
			},

			// 选择报价
			selectQuote(quoteId) {
				this.selectedQuoteId = quoteId;
			},

			// 查看图片
			viewImage(image) {
				this.currentImage = this.$wanlshop.appurl(image);
				this.showImagePreview = true;
			},

			// 关闭图片预览
			closeImagePreview() {
				this.showImagePreview = false;
			},

			// 取消选择
			cancelSelect() {
				uni.navigateBack();
			},

			// 确认选择
			confirmSelect() {
				if (!this.selectedQuoteId) {
					uni.showToast({
						title: '请选择一个报价',
						icon: 'none'
					});
					return;
				}

				const selectedQuote = this.quotes.find(quote => quote.id === this.selectedQuoteId);

				uni.$uv.http.post('/inquiry/selctedQuotation', {
					quotation_id: this.selectedQuoteId
				}).then(res => {
					if (res.res.code === 1) {
						this.$wanlshop.msg('选择成功！', 2000, true, 'success')
						// 保存选择结果到本地存储
						this.saveQuoteSelectionToStorage(selectedQuote);
						// 返回询价单详情页面
						setTimeout(() => {
							uni.navigateBack();
						}, 1500)
					}
				}).catch(error => {
					console.log('error', error)
					this.$wanlshop.msg('获取失败，请稍后重试', 2000, true)
				});
			},

			// 保存报价选择结果到本地存储
			saveQuoteSelectionToStorage(quote) {
				try {
					uni.setStorageSync('wanlshop:selectedQuote:productId:' + this.productId, {
						productId: this.productId,
						quote: quote
					});
				} catch (e) {
					console.error('保存报价选择结果失败:', e);
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 30rpx;
		background-color: #f5f5f5;
		min-height: 100vh;
		box-sizing: border-box;
		padding-bottom: 140rpx;
	}

	.page-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 30rpx;
	}

	.quote-section {
		background-color: #fff;
		border-radius: 12rpx;
		padding: 30rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
	}

	.section-title {
		font-size: 30rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 24rpx;
		padding-bottom: 20rpx;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.quote-list {
		.quote-item {
			padding: 30rpx;
			border: 1rpx solid #e0e0e0;
			border-radius: 8rpx;
			margin-bottom: 24rpx;
			position: relative;

			&.selected {
				border-color: #fe6600;
				background-color: #fffaf5;
			}

			.quote-header {
				margin-bottom: 20rpx;

				.quote-user {
					font-size: 28rpx;
					font-weight: bold;
					color: #333;
				}
			}

			.quote-details {
				display: flex;
				margin-bottom: 20rpx;

				.detail-item {
					display: flex;
					margin-right: 40rpx;

					.detail-label {
						font-size: 26rpx;
						color: #666;
						margin-right: 10rpx;
					}

					.detail-value {
						font-size: 26rpx;
						color: #333;
						font-weight: 500;
					}
				}
			}

			.quote-remark {
				display: flex;
				margin-bottom: 20rpx;

				.remark-label {
					font-size: 26rpx;
					color: #666;
					margin-right: 10rpx;
					white-space: nowrap;
				}

				.remark-content {
					font-size: 26rpx;
					color: #333;
					line-height: 1.5;
				}
			}

			.quote-image {
				display: flex;
				align-items: center;

				.image-label {
					font-size: 26rpx;
					color: #666;
					margin-right: 10rpx;
				}

				.image-link {
					font-size: 26rpx;
					color: #fe6600;
				}
			}

			.quote-selector {
				position: absolute;
				top: 30rpx;
				right: 30rpx;

				.radio {
					width: 36rpx;
					height: 36rpx;
					border: 2rpx solid #e0e0e0;
					border-radius: 50%;

					&.checked {
						border-color: #fe6600;
						background-color: #fe6600;
						position: relative;

						&::after {
							content: '';
							position: absolute;
							top: 50%;
							left: 50%;
							transform: translate(-50%, -50%);
							width: 16rpx;
							height: 16rpx;
							background-color: #fff;
							border-radius: 50%;
						}
					}
				}
			}
		}
	}

	.no-quotes {
		text-align: center;
		padding: 60rpx 0;
		font-size: 28rpx;
		color: #999;
	}

	.action-buttons {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		display: flex;
		padding: 20rpx 30rpx;
		background-color: #fff;
		box-shadow: 0 -4rpx 12rpx rgba(0, 0, 0, 0.05);

		.action-btn {
			flex: 1;
			padding: 24rpx;
			text-align: center;
			border-radius: 8rpx;
			font-size: 30rpx;
			font-weight: 500;
			margin: 0 10rpx;

			&.cancel {
				background-color: #fff;
				color: #666;
				border: 1rpx solid #e0e0e0;
			}

			&.confirm {
				background-color: #fe6600;
				color: #fff;

				&.disabled {
					background-color: #ccc;
					color: #999;
				}
			}
		}
	}

	/* 图片预览弹窗样式 */
	.image-preview-popup {
		background-color: #fff;
		border-radius: 10rpx;
		width: 600rpx;
		max-width: 90vw;

		.popup-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 30rpx;
			border-bottom: 1rpx solid #f0f0f0;

			.popup-title {
				font-size: 30rpx;
				font-weight: bold;
				color: #333;
			}

			.close-btn {
				font-size: 40rpx;
				color: #999;
			}
		}

		.popup-content {
			padding: 30rpx;

			.preview-image {
				width: 100%;
				border-radius: 8rpx;
			}
		}
	}
</style>